<?php

namespace App\Http\Controllers\Home;

use App\Mail\NotifyAdmin;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use App\Models\Permission;
use Illuminate\Support\Facades\Gate;
use App\Http\Services\sendTemplateSMS;
use App\Models\Action_log;
use Excel;
use App\Models\Record;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        dd(config('front'));
        //发送邮件
        /*try {
            Mail::to('345929453@qq.com')->send(new NotifyAdmin());
        } catch (\Exception $e) {
            var_dump(123);
        }*/
        //$SMS = new sendTemplateSMS();
        //$ret = $SMS->sendTemplateSMS('18507106389',array('123456','1分钟'),'204912');
        //return $ret;
        //拿到所有的权限，用gate判断当前角色所拥有的权限
        $permissions = Permission::orderBy("id")->get();
        foreach ($permissions as $key => $value) {
            if (!Gate::check($value['name'])) {
                unset($permissions[$key]);
            }
        }
        $permissions = $permissions->toArray();
        $menus = [];
        $count = $active = 0;
        foreach ($permissions as $key => $value) {
            if ($value['pid'] == 0) {
                if ($count == 0) {
                    $active = $key;
                }
                $count++;
                //return $active;
                $menus[$key] = $value;
                foreach ($permissions as $mm => $nn) {
                    //$menus[$key]['children'] = '';
                    if ($nn['pid'] == $value['id']) {
                        $menus[$active]['active'][$nn['name']] = $value['id'];
                    }
                    if ((($nn['pid'] == $value['id']) && (strpos($nn['name'], 'index') !== false)) || (($nn['pid'] == $value['id']) && (strpos($nn['name'], 'list') !== false))) {
                        $menus[$key]['children'][] = $nn;
                    }
                }
                if (empty($menus[$key]['children'])) {
                    $menus[$key]['children'] = [];
                }
            }
        }

        //var_dump( array_has($menus[0]['active'], 'index.index'));
        //return $menus;

        return array_values($menus);

        return $menus[0]['active']['system.index'];
        return $menus[0]['active'];
        return $menus;
        return view('home');
    }

    public function show()
    {
        return $name = Route::currentRouteName();
    }
}
